package com.example.administrator.call;

import android.Manifest;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {

    private TextView textView;
    private Button button;
    private ConPeople conPeople;
    private List<Map<String, String>> list = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.CALL_PHONE}, 124);
    }

    public void testReadAllContacts() {
        Cursor cursor = this.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,
                null, null, null, null);
        int contactIdIndex = 0;
        int nameIndex = 0;

        if (cursor.getCount() > 0) {
            contactIdIndex = cursor.getColumnIndex(ContactsContract.Contacts._ID);
            nameIndex = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
        }
        while (cursor.moveToNext()) {
            //联系人ID
            String contactId = cursor.getString(contactIdIndex);
            //联系人名字
            String name = cursor.getString(nameIndex);
            //打印信息
            Log.i("地址：", contactId);
            Log.i("姓名：", name);

            //查找该联系人的phone信息

            Cursor phones = this.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                    null,
                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + contactId,
                    null, null);
            int phoneIndex = 0;
            if (phones.getCount() > 0) {
                phoneIndex = phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
            }
            while (phones.moveToNext()) {
                //联系人电话号码
                String phoneNumber = phones.getString(phoneIndex);
                //打印信息
                Log.i("号码：", phoneNumber);
            }

            /*
             * 查找该联系人的email信息

            Cursor emails = this.getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
                    null,
                    ContactsContract.CommonDataKinds.Email.CONTACT_ID + "=" + contactId,
                    null, null);
            int emailIndex = 0;
            if (emails.getCount() > 0) {
                emailIndex = emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA);
            }
            while (emails.moveToNext()) {
                //联系人Email
                String email = emails.getString(emailIndex);
                //打印信息
                Log.i("==>", email);
            }*/
        }
        ArrayList<ConPeople> arrList = new ArrayList<>();
        for (ConPeople conPeople : arrList){
            conPeople = new ConPeople();
            conPeople.setName(conPeople.getName());
            conPeople.setPhoneNub(conPeople.getPhoneNub());
            conPeople.setAddress(conPeople.getAddress());
            conPeople.setID(conPeople.getID());
            arrList.add(conPeople);
        }

      for(ConPeople conPeople : arrList) {
            HashMap<String, String> map = new HashMap<>();
            map.put("姓名：", conPeople.getName());
            map.put("号码：", conPeople.getPhoneNub());
            map.put("地址", conPeople.getAddress());
            map.put("ID号：", conPeople.getID());
        }

        textView = (TextView) findViewById(R.id.editText);
        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String tel = textView.getText().toString();

                Intent intent = new Intent();
                intent.setAction("android.intent.action.CALL");
                intent.setData(Uri.parse("tel:" + tel));

                //Intent intent=new Intent("android.intent.action.CALL", Uri.parse("tel:"+tel));

                //方法的内部会自动为intent对象设置类别:android.intent.category.DEFAULT
                startActivity(intent);


            }
        });

    }
}
